package com.service.statistics.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.service.statistics.client.CourseClient;
import com.service.statistics.client.UcenterMemberClient;
import com.service.statistics.pojo.Daily;
import com.service.statistics.mapper.DailyMapper;
import com.service.statistics.pojo.vo.SearchObj;
import com.service.statistics.service.DailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-10-19
 */
@Service
public class DailyServiceImpl extends ServiceImpl<DailyMapper, Daily> implements DailyService {

    @Autowired
    private CourseClient courseClient;

    @Autowired
    private UcenterMemberClient ucenterMemberClient;


    @Override
    public void StatisticsDailyCount(String day) {

        QueryWrapper<Daily> queryWrapper = new QueryWrapper();
        queryWrapper.eq("date_calculated", day);
        Daily daily = this.getOne(queryWrapper);
        if (daily != null) {
            this.removeById(daily.getId());
        }
        daily = new Daily();
        daily.setCourseNum(courseClient.queryRegisterCount(day));
        daily.setRegisterNum(ucenterMemberClient.queryRegisterCount(day));
        daily.setDateCalculated(day);
        daily.setGmtCreate(new Date());
        daily.setGmtModified(new Date());
        daily.setLoginNum(RandomUtils.nextInt(100, 200));
        daily.setVideoViewNum(RandomUtils.nextInt(100, 200));
        this.save(daily);


    }

    @Override
    public Map<String, Object> queryCount(SearchObj searchObj) {
        QueryWrapper<Daily> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("date_calculated", searchObj.getBegin(), searchObj.getEnd());
        queryWrapper.orderByAsc("date_calculated");
        List<Daily> list = this.list(queryWrapper);
        List<Integer> numbers = new ArrayList<>();
        List<String> dates = new ArrayList<>();

        for (Daily daily : list) {
            dates.add(daily.getDateCalculated());
            if (searchObj.getType().equals("video_view_num"))
                numbers.add(daily.getVideoViewNum());
            if (searchObj.getType().equals("login_num"))
                numbers.add(daily.getLoginNum());
            if (searchObj.getType().equals("course_num"))
                numbers.add(daily.getCourseNum());
            if (searchObj.getType().equals("register_num"))
                numbers.add(daily.getRegisterNum());

        }
        Map<String, Object> map = new HashMap<>();
        map.put("numbers", numbers);
        map.put("dates", dates);
        return map;
    }
}
